import { Button, Tag, Tooltip, Modal, message } from 'antd';
import { QuestionCircleOutlined } from '@ant-design/icons';
import type { ProColumns } from '@ant-design/pro-table';
import ProTable from '@ant-design/pro-table';
import FileApprovaUpload from '../FileApprovaUpload';
import { request, Access, useAccess } from 'umi';
import { api } from '@/global';
import dayjs from 'dayjs';
import { useRef } from 'react';
import FileReUpload from '../FileReUpload';
const confirm = Modal.confirm;
type FileListType = {
  id: string;
  fileState: number;
  fileApprover: string;
  fileCause: string;
  fileTimes: number;
  createTime: string;
  finalTime: number;
  fileId: string;
  fileName: string;
  fileOssPath: string;
  fileDescribe: string;
  fileSuffix: string;
  pathName: string;
  bucketName: string;
};

const downloadFile = (data, fileName) => {
  if (!data) {
    return;
  }
  const url = window.URL.createObjectURL(new Blob([data]));
  const link = document.createElement('a');
  link.style.display = 'none';
  link.href = url;
  link.setAttribute('download', fileName + '.pdf');
  document.body.appendChild(link);
  link.click();
};

export default () => {
  const actionRef = useRef<ActionType>();
  const access = useAccess();
  const columns: ProColumns<FileListType>[] = [
    {
      valueType: 'indexBorder',
      dataIndex: 'id',
    },
    {
      title: '文件名称',
      dataIndex: 'fileName',
    },
    {
      title: '文件描述',
      dataIndex: 'fileDescribe',
    },
    {
      title: '所上传的文件夹',
      dataIndex: 'pathName',
    },
    {
      title: '所上传的文件筒',
      dataIndex: 'bucketName',
    },
    {
      title: '文件创建审批时间',
      dataIndex: 'createTime',
      valueType: 'date',
      sorter: (a: FileListType, b: FileListType) => {
        const acreate = dayjs(a.createTime).valueOf();
        const bcreate = dayjs(b.createTime).valueOf();
        return acreate - bcreate;
      },
    },
    {
      title: '状态',

      dataIndex: 'status',
      render: (_, b) => {
        if (b.fileState === 0) {
          return <Tag color="#bdc3c7">取消审批</Tag>;
        }
        if (b.fileState === 1) {
          return <Tag color="#74b9ff">审批中</Tag>;
        }
        if (b.fileState === 2) {
          return <Tag color="#2ed573">审批通过</Tag>;
        }
        if (b.fileState === 3) {
          return <Tag color="#ff6348">审批未通过</Tag>;
        }
      },
    },
    {
      title: '审批负责人',
      dataIndex: 'fileApprover',
    },

    {
      title: '审批人备注',
      dataIndex: 'fileCause',
    },
    {
      title: '审批通过时间',
      dataIndex: 'finalTime',
    },

    {
      title: '下载',
      width: 60,
      render: (_, b) => {
        return (
          <Access key={b.id} accessible={access.normalButtonFilter('PRIVATE_FILE:DELETE')}>
            <a
              onClick={() => {
                request(api.addFileDownload, {
                  params: {
                    id: b.id,
                  },
                });

                if (b.fileSuffix === 'pdf') {
                  downloadFile(b.fileOssPath, b.fileName);
                } else {
                  window.open(b.fileOssPath);
                }
              }}
              key={b.fileOssPath + b.fileState}
            >
              下载
            </a>
          </Access>
        );
      },
    },
    {
      title: '操作',
      width: 240,
      key: 'option',
      valueType: 'option',
      render: (_, b) => [
        b.fileState != 0 && b.fileState != 2 && (
          <a
            key={b.createTime + b.fileOssPath}
            onClick={() => {
              confirm({
                title: '确定是否取消' + b.fileName + '的审批任务',

                okText: '确定',
                okType: 'danger',
                cancelText: '取消',

                onOk: async () => {
                  await request(api.cancelFileApprove, {
                    params: {
                      fileId: b.id,
                    },
                  });
                  message.success('已经取消');
                  actionRef.current?.reload();
                },
                onCancel() {},
              });
            }}
          >
            取消审批
          </a>
        ),
        b.fileState != 0 && (
          <a
            key={'heiheihei' + b.id}
            onClick={() => {
              message.success('催促成功！工作人员正在努力核实');
            }}
          >
            催促一下
          </a>
        ),
        b.fileState != 0 && b.fileState === 3 && (
          <FileReUpload
            key={'reload' + b.id}
            id={b.id}
            fileId={b.fileId}
            getAllFileInfo={() => {
              actionRef.current?.reload();
            }}
          />
        ),
      ],
    },
  ];

  const reload = () => {
    actionRef.current?.reload();
  };
  return (
    <ProTable<FileListType>
      tooltip="该模块负责教师和学生向非私有文件夹内存放资源，用途：教师需要将科研报告放入到科研的文件下，就需要本系部或者更高领导级别的审批"
      actionRef={actionRef}
      columns={columns}
      request={async (params, sorter, filter) => {
        const result = await request(api.getPrivateApproveFileList);

        return result;
      }}
      rowKey="id"
      pagination={{
        showQuickJumper: true,
      }}
      search={false}
      dateFormatter="string"
      headerTitle="文件审批列表"
      toolBarRender={() => [<FileApprovaUpload getAllFileInfo={reload}></FileApprovaUpload>]}
    />
  );
};
